function createAccount(type) {
    let data = {};
    if (type === 'single') {
        let account = document.getElementById("single-account").value.trim();
        if (account === "") {
            showToast("请输入账号");
            return;
        }
        data = {
            type: "single",
            account: account,
            password: document.getElementById("single-password").value.trim()
        };
    } else if (type === 'batch') {
        let startAccount = document.getElementById("batch-start-account").value.trim();
        let endAccount = document.getElementById("batch-end-account").value.trim();
        if (startAccount === "" || endAccount === "") {
            showToast("请输入起始和结束账号");
            return;
        }
        data = {
            type: "batch",
            startAccount: startAccount,
            endAccount: endAccount,
            password: document.getElementById("batch-password").value.trim()
        };
    } else {
        showToast("无效的类型");
        return;
    }
    fetch("/api/account/create", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(data)
    })
        .then(res => res.json())
        .then(data => {
            document.getElementById("account-result").innerText = data.message;
        })
        .catch(err => console.error(err));
}

function deleteAccount(type) {
    let data = {};
    if (type === 'single') {
        let account = document.getElementById("single-account").value.trim();
        if (account === "") {
            showToast("请输入账号");
            return;
        }
        data = {
            type: "single",
            account: account
        };
    } else if (type === 'batch') {
        let startAccount = document.getElementById("batch-start-account").value.trim();
        let endAccount = document.getElementById("batch-end-account").value.trim();
        if (startAccount === "" || endAccount === "") {
            showToast("请输入起始和结束账号");
            return;
        }
        data = {
            type: "batch",
            startAccount: startAccount,
            endAccount: endAccount
        };
    } else {
        showToast("无效的类型");
        return;
    }
    fetch("/api/account/delete", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(data)
    })
        .then(res => res.json())
        .then(data => {
            showToast(data.message);
        })
        .catch(err => console.error(err));
}

function updatePassword(type) {
    let data = {};
    if (type === 'single') {
        let account = document.getElementById("single-account").value.trim();
        let newPassword = document.getElementById("single-password").value.trim();
        if (account === "" || newPassword === "") {
            document.getElementById("account-result").innerText = "请输入账号或新密码";
            return;
        }
        data = {
            type: "single",
            account: account,
            newPassword: newPassword
        };
    } else if (type === 'batch') {
        let startAccount = document.getElementById("batch-start-account").value.trim();
        let endAccount = document.getElementById("batch-end-account").value.trim();
        let newPassword = document.getElementById("batch-password").value.trim()
        if (startAccount === "" || endAccount === "" || newPassword === "") {
            document.getElementById("account-result").innerText = "请输入起始、结束账号和新密码";
            return;
        }
        data = {
            type: "batch",
            startAccount: startAccount,
            endAccount: endAccount,
            newPassword: newPassword
        };
    } else {
        document.getElementById("account-result").innerText = "无效的类型";
        return;
    }
    fetch("/api/account/updatePassword", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(data)
    })
        .then(res => res.json())
        .then(data => {
            document.getElementById("account-result").innerText = data.message;
        })
        .catch(err => console.error(err));
}

function addAdminUser(){
    let account = document.getElementById("admin-account").value.trim();
    let password = document.getElementById("admin-password").value.trim();
    if (account === "" || password === "") {
        showToast("请输入账号和密码");
        return;
    }
    let data = {
        account: account,
        password: password
    };
    fetch("/api/admin/addUser", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(data)
    })
        .then(res => res.json())
        .then(data => {
            showToast(data.message);
        })
        .catch(err => console.error(err));
}

function deleteAdminUser() {
    let account = document.getElementById("admin-account").value.trim();
    if (account === "") {
        showToast("请输入账号");
        return;
    }
    let data = {
        account: account
    };
    fetch("/api/admin/delUser", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(data)
    })
        .then(res => res.json())
        .then(data => {
            showToast(data.message);
        })
        .catch(err => console.error(err));
}

function kickAccountDevice() {
    let account = document.getElementById("kick-account").value.trim();
    let device = document.getElementById("kick-device").value.trim();
    if (account === "" || device === "") {
        showToast("请输入账号和设备")
        return;
    }
    fetch("/api/account/kick", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ account, device })
    })
        .then(res => res.json())
        .then(data => {
            showToast(data.message)
        })
        .catch(err => console.error(err));
}